#include<stdio.h>
#include<string.h>
char prog[105][15];
char vars[105];
int jmptbl[105];
int loopStack[105];
int lsi = 0;
int vari;
int lines;
int guess;
int has(char x) {
  for(int i=0; i<vari; ++i) if(vars[i]==x) return 1;
  return 0;
}
void push(char x) { vars[vari++] = x; }
void pop() { vari--; }
void clear() { vari = 0; }
int syntax_check() {
  clear();
  int left = 0;
  for(int i=0; i<lines; ++i) {
    if(prog[i][0]=='F') {
      char var = prog[i][2];
      if(has(var)) return -1;
      push(var);
      left++;
    }
    else {
      pop();
      left--;
    }
    if(left < 0) return -1;
  }
  if(left!=0) return -1;
  return 0;
}
void build_jump_table() {
  memset(jmptbl, 0, (lines)*sizeof(int));
  clear();
  for(int i=lines-1; i>=0; --i) {
    if(prog[i][0]=='E') push(i);
    else jmptbl[i] = vars[vari-1], pop();
  }
}
void debug() {
  printf("guess: %d\n", guess);
  for(int i=0; i<lines; ++i) {
    printf("%d %s", jmptbl[i], prog[i]);
  }
}
int greater(char *a, char *b) {
  int x, y;
  if(sscanf(a, "%d", &x)!=1) return 0;
  if(sscanf(b, "%d", &y)!=1) return 0;
  return x>=y;
}
void solve() {
  lsi = 0;
  scanf("%d", &lines);
  for(int i=0; i<3; ++i) getchar();
  if(getchar() == '1') guess=0;
  else scanf("^%d", &guess);
  scanf(")\n");
  for(int i=0; i<lines; ++i) {
    fgets(prog[i], 105, stdin);
  }
  if(syntax_check()!=0) {
    puts("ERR"); return;
  }
  build_jump_table();
#ifndef ONLINE_JUDGE
  // debug();
#endif
  int loop = 0, ans = 0;
  for(int i=0; i<lines; ++i) {
    char x[5], y[5];
    if(prog[i][0]=='E') lsi--;
    else {
      sscanf(prog[i], "F %*s %s %s", x, y);
      if(x[0]!='n' && y[0]=='n') loopStack[lsi++] = 1;
      else if(x[0]=='n' && y[0]!='n' || greater(x, y)) {
        i = jmptbl[i];
      }
      else loopStack[lsi++] = 0;
    }
    loop = 0;
    for(int j=0; j<lsi; ++j) loop+=loopStack[j];
    ans = ans>loop? ans:loop;
  }
#ifndef ONLINE_JUDGE
  printf("ans :%d, guess: %d\n", ans, guess);
#endif
  printf(ans==guess? "Yes\n" : "No\n");
}
int main() {
  // freopen("P3952_6.in", "r", stdin);
  int tc; scanf("%d\n", &tc);
  for(int t=0; t<tc; ++t)
    solve();
}

